from typing import Any

from rest_framework.response import Response
from rest_framework import status as rest_status


class JsonResponse(Response):
    def __init__(self, success: bool = True, code: int = rest_status.HTTP_200_OK, msg: str = "", data: Any = None,
                 headers: dict = None, content_type: str = None, status: int = rest_status.HTTP_200_OK):
        if data is None:
            data = {}

        if success is False and code == rest_status.HTTP_200_OK:
            code = rest_status.HTTP_400_BAD_REQUEST

        ret = {
            'success': success,
            'code': code,
            'msg': msg,
            'data': data
        }

        super().__init__(data=ret, status=status,
                         template_name=None, headers=headers,
                         exception=False, content_type=content_type)
